/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang Changxing<ZhangChangxingVIP@163.com>
 *                  All rights reserved.
 *
 *       Filename:  platform_conf.c
 *    Description:  This file is used to configure platform information.
 *                 
 *        Version:  1.0.0(05/1/2022)
 *         Author:  Zhang Changxing <ZhangChangxingVIP@163.com>
 *      ChangeLog:  1, Release initial version on "05/1/2022  01:33:25 AM"
 *                 
 ********************************************************************************/

#include "main.h"


int inifile_parsing(iotd_ctx_t *iotd_ctx, int platform)
{
    dictionary  *ini = NULL;
    const char  *str = NULL;
    char        buff[1024];
    int	        port;
    char        *ini_path = NULL;
    char        section[32];
    char        *ptr_platform = NULL;

    mqtt_connect_ctx_t *mqtt_connect_ctx = &iotd_ctx->mqtt_connect_ctx;
    pub_ctx_t   *pub_ctx = &iotd_ctx->pub_ctx;
    log_ctx_t   *log_ctx = &iotd_ctx->log_ctx;

    if(!iotd_ctx)
    {
        log_error("The %s() arguments input error!\n", __func__);
        return -1;
    }
    
    pub_ctx->platform_flag =    platform;
    mqtt_connect_ctx->platform_flag = platform;

    ini_path = "../conf_file/conf.ini";

    switch (platform)
    {
        case ALI:
        {
            ptr_platform = "ali";
            break;
        }
        case TENGCENT:
        {
            ptr_platform = "tengcent";
            break;
        }
        case HUAWEI:
        {
            ptr_platform = "huawei";
            break;
        }
    }

    ini = iniparser_load(ini_path);
    if( ini == NULL )
    {
        return -1;
    }
    
    /*Initialize the mqtt_connect_ctx structure*/
    snprintf(section, 32, "%s:host",ptr_platform);
    if(!(str=iniparser_getstring(ini, section, NULL)))
    {
        log_error("ERROR: Parser failed to obtain the host!\n");
        return -1;
    }
    strncpy(mqtt_connect_ctx->host, str, strlen(str));

    snprintf(section, 32, "%s:port",ptr_platform);
    if((mqtt_connect_ctx->port = iniparser_getint(ini, section, -1)) == -1)
    {
        log_error("ERROR: Parser failed to obtain the post!\n");
        return -2;
    }
 
    snprintf(section, 32, "%s:username",ptr_platform);
    if(!(str = iniparser_getstring(ini, section, NULL)))
    {
        log_error("ERROR: Parser failed to obtain the username!\n");
        return -3;
    }
    strncpy(mqtt_connect_ctx->username, str, strlen(str));

    snprintf(section, 32, "%s:passwd",ptr_platform);
    if(!(str = iniparser_getstring(ini, section, NULL)))
    {
        log_error("ERROR: Parser failed to obtain the passwd!\n");
        return -4;
    }
    strncpy(mqtt_connect_ctx->passwd, str, strlen(str));

    snprintf(section, 32, "%s:clientid",ptr_platform);
    if(!(str = iniparser_getstring(ini, section, NULL)))
    {
        log_error("ERROR: Parser failed to obtain the clientid!\n");
        return -5;
    }
    strncpy(mqtt_connect_ctx->client_id, str, strlen(str));

    snprintf(section, 32, "%s:keepalive",ptr_platform);
    if((mqtt_connect_ctx->keepalive = iniparser_getint(ini, section, -1)) == -1)
    {
        log_error("ERROR: Parser failed to obtain the keepalive!\n");
        return -6;
    }

    /*Initialize the pub_ctx structure*/
    snprintf(section, 32, "%s:pub_topic",ptr_platform);
    if(!(str = iniparser_getstring(ini, section, NULL)))
    {
        log_error("ERROR: Parser failed to obtain the pub_topic!\n");
        return -8;
    }
    strncpy(pub_ctx->pub_topic, str, strlen(str));

    snprintf(section, 32, "%s:pub_qos",ptr_platform);   
    if((pub_ctx->pub_qos = iniparser_getint(ini, section, -1)) == -1)
    {
        log_error("ERROR: Parser failed to obtain the qos!\n");
        return -7;
    }


    /*Initialize the log_ctx structure*/
    snprintf(section, 32, "logger:loglevel");
    if((log_ctx->loglevel = iniparser_getint(ini, section, -1)) == -1)
    {
        log_error("ERROR: Parser failed to obtain the qos!\n");
        return -10;
    }

    snprintf(section, 32, "logger:logfile");
    if(!(str = iniparser_getstring(ini, section, NULL)))
    {
        log_error("ERROR: Parser failed to obtain the logfile!\n");
        return -9;
    }
    strncpy(log_ctx->logfile, str, strlen(str));

    snprintf(section, 32, "logger:logsize");
    if((log_ctx->logsize = iniparser_getint(ini, section, -1)) == -1)
    {
        log_error("ERROR: Parser failed to obtain the logsize!\n");
        return -10;
    }

    return 0;

}


